package com.aurora.oa.work.view;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.adapter.CommonRecyclerViewAdapter;
import com.aurora.oa.adapter.CommonRecyclerViewHolder;
import com.aurora.oa.bean.HttpResult;
import com.aurora.oa.customview.ItemDivider;
import com.aurora.oa.listener.OnRequestHttpDataListener;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.utils.ToastUtil;
import com.aurora.oa.work.bean.FunctionItemBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/4/19 0019.
 */
public class ApplyMainActivity extends BaseActivity {

    @Bind(R.id.tv_attendance_number)
    TextView tvAttendanceNumber;
    @Bind(R.id.tv_mysend_number)
    TextView tvMysendNumber;
    @Bind(R.id.rv_apply_type)
    RecyclerView rvApplyType;
    @Bind(R.id.ll_attendance_total)
    LinearLayout llAttendanceTotal;
    @Bind(R.id.ll_mysend_apply)
    LinearLayout llMysendApply;
    private RequestHttpModel model = new RequestHttpModelImpl();
    private CommonRecyclerViewAdapter<FunctionItemBean> commonRecyclerViewAdapter;
    private List<FunctionItemBean> functionItems = new ArrayList<>();
    private int[] imagesIDs = new int[]{R.mipmap.ioc_sp_bx, R.mipmap.ioc_sp_ty, R.mipmap.ioc_sp_yc,
            R.mipmap.ioc_sp_qj, R.mipmap.ioc_sp_jb, R.mipmap.ioc_sp_cc, R.mipmap.ioc_sp_tx,
            R.mipmap.ioc_sp_zz,};
    private String[] functionName = new String[]{
            "报销", "通用", "用车", "请假", "加班", "出差", "调休", "转账"
    };
    private GridLayoutManager gridLayoutManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setMyContentView(R.layout.activity_apply_main);
        ButterKnife.bind(this);
        initTitlebar(true, "申请", true);
        initView();
        initData();
    }

    private void initData() {
        //approvalCenter
        model.getApplyMainUIData(new OnRequestHttpDataListener() {
            @Override
            public void onRequestDataSuccess(String body) {
                Log.e("ApplyCenter", body);
                String result = body;

                //{"msg":"查询成功","obj":{"signNumber":0,"approvalNumber":8,"myApprovalNumber":28},"success":true}
                Gson gson = new Gson();
                Type type = new TypeToken<HttpResult<UIData>>() {
                }.getType();
                HttpResult<UIData> dataHttpResult = gson.fromJson(result, type);

                if (dataHttpResult == null || !dataHttpResult.success) {
                    ToastUtil.show(getApplicationContext(), "请求失败");
                    return;
                }

                if (dataHttpResult.obj != null) {
                    tvAttendanceNumber.setText(dataHttpResult.obj.approvalNumber + "");
                    tvMysendNumber.setText(dataHttpResult.obj.myApprovalNumber + "");
                }

            }

            @Override
            public void onRequestDataFail(String message) {
                ToastUtil.show(getApplicationContext(), message);
            }
        });
    }

    @SuppressWarnings("ResourceType")
    private void initView() {
        for (int i = 0, length = imagesIDs.length; i < length; i++) {
            FunctionItemBean bean = new FunctionItemBean();
            bean.mipmapID = imagesIDs[i];
            bean.title = functionName[i];
            functionItems.add(bean);
        }

        commonRecyclerViewAdapter = new CommonRecyclerViewAdapter<FunctionItemBean>(this, functionItems) {
            @Override
            public void convert(CommonRecyclerViewHolder h, FunctionItemBean entity, int position) {
                h.setText(R.id.tv_function, entity.title);
                h.setImage(R.id.iv_function, entity.mipmapID);
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_function;
            }
        };

        gridLayoutManager = new GridLayoutManager(this, 3);
        rvApplyType.setLayoutManager(gridLayoutManager);
        rvApplyType.setAdapter(commonRecyclerViewAdapter);
        rvApplyType.addItemDecoration(new ItemDivider().setDividerWith(5).setDividerColor(Color.parseColor(getString(R.color.background_gray))));
        commonRecyclerViewAdapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                switch (functionName[position]) {
                    case "报销":
                        startActivity(new Intent(ApplyMainActivity.this, WorkExpenceMainActivity.class));
                        break;
                    case "通用":
                        startActivity(new Intent(ApplyMainActivity.this, ApplyCommonActivity.class));
                        break;
                    case "用车":
                        startActivity(new Intent(ApplyMainActivity.this, ApplyCarListActivity.class));
                        break;
                    case "请假":
                        startActivity(new Intent(ApplyMainActivity.this, ApplyLeaveActivity.class));
                        break;
                    case "加班":
                        startActivity(new Intent(ApplyMainActivity.this, ApplyOverTimeActivity.class));
                        break;
                    case "出差":
                        startActivity(new Intent(ApplyMainActivity.this, ApplyEvectionActivity.class));
                        break;
                    case "调休":
                        startActivity(new Intent(ApplyMainActivity.this, ApplyWorkDayOffActivity.class));
                        break;
                    case "转账":
                        startActivity(new Intent(ApplyMainActivity.this, ApplyMoneyActivity.class));
                        break;

                }
            }
        });

    }

    @OnClick({R.id.ll_attendance_total, R.id.ll_mysend_apply})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_attendance_total:
                startActivity(new Intent(ApplyMainActivity.this, AttendancePersonActivity.class));
                break;
            case R.id.ll_mysend_apply:
                startActivity(new Intent(ApplyMainActivity.this, ApplyMySendListActivity.class));
                break;
        }
    }

    private class UIData {
        //签到人数
        public int signNumber;
        //待我审批
        public int approvalNumber;
        //我发起的
        public int myApprovalNumber;
    }
}
